package org.example.reemploymentsys.controller;

import org.example.reemploymentsys.entity.Exam;
import org.example.reemploymentsys.entity.ExamRecord;
import org.example.reemploymentsys.entity.Organization;
import org.example.reemploymentsys.entity.Student;
import org.example.reemploymentsys.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ExamSituationController {
    @Autowired
    private ExamRecordRe examRecordRe;
    @Autowired
    private ExamRe examRe;
    @Autowired
    private StudentRe studentRe;
    @Autowired
    private UserTbRe userTbRe;
    @Autowired
    private OrganizationRe organizationRe;
    @GetMapping("/getExamResult")
    public Page<ExamRecord> getExamResult(
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize
    ){
        System.out.println(username+keyword+page+pageSize);
        String OrganizationId= userTbRe.findOrgizationIdByUsername(username);
        Organization organization = organizationRe.findOrganizationById(OrganizationId);
        PageRequest pageRequest = PageRequest.of(page-1 , pageSize);
        Exam exam=examRe.findByName(keyword);
        Student student=studentRe.findByName(keyword);
        if(keyword != null && !keyword.isEmpty()){
            System.out.println(examRecordRe.findAllByStudentOrExam(student,exam,pageRequest));
            return examRecordRe.findAllByStudentOrExam(student,exam,pageRequest);
        }else {
            System.out.println(examRecordRe.findAllByStudent_Organization(organization,pageRequest));
            return examRecordRe.findAllByStudent_Organization(organization,pageRequest);
        }
    }
}
